package com.gzyj.jtt808.v2011.protocol.common;

import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.utils.IntUtils;
import com.gzyj.jtt808.v2011.enums.AlarmFlag;
import com.gzyj.jtt808.v2011.enums.StatusBit;
import io.netty.buffer.ByteBuf;
import lombok.Data;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lhj
 */
@Data
public class GnssDataBasicInfo implements Serializable {
    private List<AlarmFlag> ALARMFLAG;
    private List<StatusBit> STATUSBIT;
    private int LAT;//纬度
    private int LNG;//经度
    private short ALTITUDE;//海拔
    private short SPEED;//速度
    private short DIRECTION;//方向
    private String TIME;//时间


    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public int datalen() {
        return 28;
    }

    public void encode(ByteBuf buf) {
        if (this.ALARMFLAG.size() > 0) {
            int sum = 0;
            for (AlarmFlag flag : ALARMFLAG) {
                sum += flag.getCode();
            }
            buf.writeInt(sum);


        }

        if (this.STATUSBIT.size() > 0) {
            int sum = 0;
            for (StatusBit flag : STATUSBIT) {
                sum += flag.getCode();
            }
            buf.writeInt(sum);


        }

        buf.writeInt(this.LAT);
        buf.writeInt(this.LNG);
        buf.writeShort(this.ALTITUDE);
        buf.writeShort(this.SPEED);
        buf.writeShort(this.DIRECTION);

        try {
            Date date = sdf.parse(this.TIME);
            byte[] temp= HexBytesUtil.WriteDateTime_yyMMddHHmmss(date);
            buf.writeBytes(temp);

        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

    public GnssDataBasicInfo decode(ByteBuf buf){
        byte[] temp=HexBytesUtil.readByOffset(buf,4);


        this.ALARMFLAG=new ArrayList<>();
        String str= IntUtils.intToBinary(HexBytesUtil.byteArrayToInt(temp),32);

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == '1') {
                int res = new Double(Math.pow(2, (32 - i - 1))).intValue();
                ALARMFLAG.add(AlarmFlag.find(res));
            }
        }
        temp=HexBytesUtil.readByOffset(buf,4);
        this.STATUSBIT=new ArrayList<>();
        str= IntUtils.intToBinary(HexBytesUtil.byteArrayToInt(temp),32);

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == '1') {
                int res = new Double(Math.pow(2, (32 - i - 1))).intValue();
                STATUSBIT.add(StatusBit.find(res));
            }
        }

        temp=HexBytesUtil.readByOffset(buf,4);
        this.LAT=HexBytesUtil.byteArrayToInt(temp);
        temp=HexBytesUtil.readByOffset(buf,4);
        this.LNG=HexBytesUtil.byteArrayToInt(temp);
        temp=HexBytesUtil.readByOffset(buf,2);
        this.ALTITUDE=Short.parseShort(HexBytesUtil.bytesToHex(temp),16);
        temp=HexBytesUtil.readByOffset(buf,2);
        this.SPEED=Short.parseShort(HexBytesUtil.bytesToHex(temp),16);
        temp=HexBytesUtil.readByOffset(buf,2);
        this.DIRECTION=Short.parseShort(HexBytesUtil.bytesToHex(temp),16);
        temp=HexBytesUtil.readByOffset(buf,6);
        this.TIME=HexBytesUtil.ReadDateTime_yyMMddHHmmss(temp);

        return this;
    }
}
